package pl.kosmowska.ewelina.client.views.widgets.grids;

import java.util.Comparator;
import java.util.Date;

import pl.kosmowska.ewelina.client.app.requestfactory.dto.LoanDataProxy;

import com.google.gwt.cell.client.CheckboxCell;
import com.google.gwt.cell.client.DateCell;
import com.google.gwt.cell.client.NumberCell;
import com.google.gwt.cell.client.TextCell;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.safehtml.shared.SafeHtmlUtils;
import com.google.gwt.user.cellview.client.Column;
import com.google.gwt.user.cellview.client.ColumnSortEvent.ListHandler;
import com.google.gwt.user.cellview.client.DataGrid;
import com.google.gwt.view.client.DefaultSelectionEventManager;
import com.google.gwt.view.client.ProvidesKey;
import com.google.gwt.view.client.SelectionChangeEvent;
import com.google.gwt.view.client.SingleSelectionModel;


public class LoanDataGrid<T> extends PagingDataGrid<T>{
    final ProvidesKey<T> KEY_PROVIDER = new ProvidesKey<T>() {
        public Object getKey(T item) {
          return item == null ? null : ((LoanDataProxy)item).getId();
        }
      };
	
	final SingleSelectionModel<T> selectionModel =new SingleSelectionModel<T>(KEY_PROVIDER);
	
	T selectedItem =null;
	
	public T getSelectedItem() {
		return selectedItem;
	}

	@Override
	public void initTableColumns(DataGrid<T> dataGrid,
			ListHandler<T> sortHandler) {
		
	    final ProvidesKey<T> KEY_PROVIDER = new ProvidesKey<T>() {
	        public Object getKey(T item) {
	          return item == null ? null : ((LoanDataProxy)item).getId();
	        }
	      };
	    // Add a selection model so we can select cells.
	     final SingleSelectionModel<T> selectionModel =new SingleSelectionModel<T>(KEY_PROVIDER);
	    selectionModel.addSelectionChangeHandler(new SelectionChangeEvent.Handler() {

	        @Override
	        public void onSelectionChange(SelectionChangeEvent event) {
	            selectedItem = selectionModel.getSelectedObject();
	        }
	    });
	    dataGrid.setSelectionModel(selectionModel, DefaultSelectionEventManager
	            .<T> createCheckboxManager());
	    
	    // Checkbox column. This table will uses a checkbox column for selection.
	    // Alternatively, you can call dataGrid.setSelectionEnabled(true) to enable
	    // mouse selection.
	    Column<T, Boolean> checkColumn =
	        new Column<T, Boolean>(new CheckboxCell(true, false)) {
	          @Override
	          public Boolean getValue(T object) {
	            // Get the value from the selection model.
	            return selectionModel.isSelected(object);
	          }
	        };
	    dataGrid.addColumn(checkColumn, SafeHtmlUtils.fromSafeConstant("<br/>"));
	    dataGrid.setColumnWidth(checkColumn,5, Unit.PCT);
	    

		Column<T, String> firstNameColumn = new Column<T, String>(
				new TextCell()) {
			@Override
			public String getValue(T object) {
				return ((LoanDataProxy) object).getName();
			}
		};
		firstNameColumn.setSortable(true);
		sortHandler.setComparator(firstNameColumn, new Comparator<T>() {
			public int compare(T o1, T o2) {
				return ((LoanDataProxy) o1).getName().compareTo(
						((LoanDataProxy) o2).getName());
			}
		});
		dataGrid.addColumn(firstNameColumn, "Nazwa");

		dataGrid.setColumnWidth(firstNameColumn, 5, Unit.PCT);

		//Value column
		Column<T, Number> valueColumn = new Column<T, Number>(new NumberCell()) {
			@Override
			public Number getValue(T object) {
				return ((LoanDataProxy) object).getValue();
			}
		};
		valueColumn.setSortable(false);
		dataGrid.addColumn(valueColumn, "Wartosc");
		dataGrid.setColumnWidth(valueColumn, 5, Unit.PCT);
		
		// Date column
		Column<T, Date> dateStartColumn = new Column<T, Date>(new DateCell()) {
			@Override
			public Date getValue(T object) {
				return ((LoanDataProxy) object).getStartDate();
			}
		};
		dateStartColumn.setSortable(true);
		sortHandler.setComparator(dateStartColumn, new Comparator<T>() {
			public int compare(T o1, T o2) {
				return ((LoanDataProxy) o1).getStartDate().compareTo(
						((LoanDataProxy) o2).getStartDate());
			}
		});
		dataGrid.addColumn(dateStartColumn, "Data poczatkowa");
		dataGrid.setColumnWidth(dateStartColumn,10, Unit.PCT);
		
		Column<T, Date> dateEndColumn = new Column<T, Date>(new DateCell()) {
			@Override
			public Date getValue(T object) {
				return ((LoanDataProxy) object).getEndDate();
			}
		};
		dateEndColumn.setSortable(true);
		sortHandler.setComparator(dateEndColumn, new Comparator<T>() {
			public int compare(T o1, T o2) {
				return ((LoanDataProxy) o1).getEndDate().compareTo(
						((LoanDataProxy) o2).getEndDate());
			}
		});
		dataGrid.addColumn(dateEndColumn, "Data koncowa");
		dataGrid.setColumnWidth(dateEndColumn,10, Unit.PCT);
		
		//Value column
		Column<T, Number> percentageColumn = new Column<T, Number>(new NumberCell()) {
			@Override
			public Number getValue(T object) {
				return ((LoanDataProxy) object).getPercentage();
			}
		};
		valueColumn.setSortable(false);
		dataGrid.addColumn(percentageColumn, "Procent");
		dataGrid.setColumnWidth(percentageColumn, 5, Unit.PCT);
		

		
		//Value column
		Column<T, Number> monthNumberColumn = new Column<T, Number>(new NumberCell()) {
			@Override
			public Number getValue(T object) {
				return ((LoanDataProxy) object).getMonthNumber();
			}
		};
		valueColumn.setSortable(false);
		dataGrid.addColumn(monthNumberColumn, "Dlugosc (miesiace)");
		dataGrid.setColumnWidth(monthNumberColumn, 10, Unit.PCT);
		
		Column<T, Number> valuePaidColumn = new Column<T, Number>(new NumberCell()) {
			@Override
			public Number getValue(T object) {
				return ((LoanDataProxy) object).getValuePaid();
			}
		};
		valuePaidColumn.setSortable(false);
		dataGrid.addColumn(valuePaidColumn, "Dotychczas zaplacono");
		dataGrid.setColumnWidth(valuePaidColumn, 10, Unit.PCT);
		
		Column<T, Number> valueNotPaidColumn = new Column<T, Number>(new NumberCell()) {
			@Override
			public Number getValue(T object) {
				return ((LoanDataProxy) object).getValueNotPaid();
			}
		};
		valueNotPaidColumn.setSortable(false);
		dataGrid.addColumn(valueNotPaidColumn, "Pozostalo do zaplacenia");
		dataGrid.setColumnWidth(valueNotPaidColumn, 10, Unit.PCT);
	

	}
	
	public SingleSelectionModel<T> getSelectionModel (){
		return selectionModel;
	}

}

